<template>
	<view class="container">
		<view class="con_auto">
			<u--form labelPosition="top" :model="dataForm" :rules="rules" ref="uForm" labelWidth="150">
				<u-form-item label="注册会员手机号一致" prop="" borderBottom ref="item1">
					<u--input v-model="dataForm.mobile" :disabled="is_update" placeholder="请输入手机号"></u--input>
				</u-form-item>
				<u-form-item label="珠宝系统编号" prop="" borderBottom ref="item1">
					<u--input v-model="dataForm.user_code" :disabled="is_update" placeholder="请输入系统编号"></u--input>
				</u-form-item>
				<u-form-item label="发送欢迎语" prop="" borderBottom ref="item1" v-if="false">
					<u-switch
						v-model="dataForm.auto_send"
						asyncChange
						:activeValue="1"
						:inactiveValue="0"
						@change="asyncChange('auto_send')"
						:loading="auto_send_loading"
					></u-switch>
				</u-form-item>
				<u-form-item label="欢迎语" prop="" borderBottom ref="item1" @click="getTempList('auto_send')" v-if="false">
					<u--input disabled v-model="dataForm.auto_send_name"></u--input>
				</u-form-item>
				<block v-if="false">
					<u-form-item label="三日消息" prop="userInfo.name" borderBottom ref="item1">
						<u-switch
							v-model="dataForm.send_day"
							asyncChange
							:activeValue="1"
							:inactiveValue="0"
							@change="asyncChange('send_day')"
							:loading="send_day_loading"
						></u-switch>
					</u-form-item>
					<u-form-item label="三日" prop="" borderBottom ref="item1" v-if="dataForm.send_day">
						<u--input v-model="dataForm.send_day_name"></u--input>
					</u-form-item>
					<u-form-item label="三月消息" prop="userInfo.name" borderBottom ref="item1">
						<u-switch
							v-model="dataForm.send_month"
							asyncChange
							:activeValue="1"
							:inactiveValue="0"
							@change="asyncChange('send_month')"
							:loading="send_month_loading"
						></u-switch>
					</u-form-item>
					<u-form-item label="三月" prop="" borderBottom ref="item1" v-if="dataForm.send_month">
						<u--input v-model="dataForm.send_month_name"></u--input>
					</u-form-item>
					<u-form-item label="三年消息" prop="userInfo.name" borderBottom ref="item1">
						<u-switch
							v-model="dataForm.send_year"
							asyncChange
							:activeValue="1"
							:inactiveValue="0"
							@change="asyncChange('send_year')"
							:loading="send_year_loading"
						></u-switch>
					</u-form-item>
					<u-form-item label="三年" prop="" borderBottom ref="item1" v-if="dataForm.send_year">
						<u--input v-model="dataForm.send_year_name"></u--input>
					</u-form-item>
					<u-form-item label="纪念日消息" prop="userInfo.name" borderBottom ref="item1">
						<u-switch
							v-model="dataForm.send_mark"
							asyncChange
							:activeValue="1"
							:inactiveValue="0"
							@change="asyncChange('send_mark')"
							:loading="send_mark_loading"
						></u-switch>
					</u-form-item>
					<u-form-item label="纪念日" prop="" borderBottom ref="item1" v-if="dataForm.send_mark">
						<u--input v-model="dataForm.send_mark_name"></u--input>
					</u-form-item>
					<u-form-item label="生日消息" prop="userInfo.name" borderBottom ref="item1">
						<u-switch
							v-model="dataForm.send_birthday"
							asyncChange
							:activeValue="1"
							:inactiveValue="0"
							@change="asyncChange('send_birthday')"
							:loading="send_mark_loading"
						></u-switch>
					</u-form-item>
					<u-form-item label="生日" prop="" borderBottom ref="item1" v-if="dataForm.send_birthday">
						<u--input v-model="dataForm.send_birthday_name"></u--input>
					</u-form-item>
				</block>
				<u-picker :show="showSelect" :columns="selectColumns" keyName="title" @cancel="cancleSelect" @confirm="confirmSelect"></u-picker>
			</u--form>
			<view class="btn">
				<u-button text="保存" type="primary" @tap.stop="submit"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			showSelect: false,
			selectColumns: [],
			rules: {},
			dataForm: {
				auto_send: 0, //欢迎语
				auto_send_name: '', //欢迎语
				auto_send_id: 0,
				send_day: 0, //三日
				send_day_name: '', //三日
				send_day_id: 0,
				send_month: 0, //三月
				send_month_name: '', //三月
				send_month_id: 0,
				send_year: 0, //三年
				send_year_name: '', //三年
				send_year_id: 0,
				send_mark: 0, //结婚纪念日
				send_mark_name: '', //结婚纪念日
				send_mark_id: 0,
				send_birthday: 0,
				mobile:'',
				user_code:''
			},
			auto_send_loading: false,
			send_day_loading: false,
			send_month_loading: false,
			send_year_loading: false,
			send_mark_loading: false,
			selectItem: '',
			is_update:false
		};
	},
	onLoad() {
		this.getInfo();
	},
	methods: {
		submit(){
			let _this = this;
			_this.$http('department_user/updateDepartmentData',{
				mobile:_this.dataForm.mobile,
				user_code:_this.dataForm.user_code
			}).then((res) => {
				if (res.code) {
					uni.$u.toast('保存成功')
					setTimeout(()=>{
						_this.$goBack()
					},1000)
				}
			});
		},
		getInfo() {
			let _this = this;
			_this.$http('department_config/getDepartmentConfigByInfo').then((res) => {
				if (res.code) {
					this.dataForm = res.data;
					if(res.data.mobile){
						_this.is_update = true
					}
				}
			});
		},
		asyncChange(type) {
			let _this = this;
			if (this.dataForm[type]) {
				_this
					.$http('department_config/setDepartmentConfigByUserInfo', {
						type,
						id: 0,
						default: 0
					})
					.then((res) => {
						if (res.code) {
							this.dataForm[type] = 0;
						}
					});
			} else {
				this[type + '_loading'] = true;
				_this.getTempList(type)
			}
		},
		async getTempList(type){
			let _this = this;
			this.selectItem = type;
			_this
				.$http('message_template/getMessageTemplateByTypeList', {
					type
				})
				.then((res) => {
					if (res.code) {
						if (res.data.length != 0) {
							_this.showSelect = true;
							_this.selectColumns = [res.data];
						}
					}
				});
		},
		confirmSelect({ indexs, value, values }) {
			let _this = this;
			let type = _this.selectItem;
			_this.dataForm[type + '_name'] = value[0]['title'];
			_this[type + '_loading'] = false;
			_this.dataForm[type + '_id'] = value[0]['id'];
			_this
				.$http('department_config/setDepartmentConfigByUserInfo', {
					type,
					id: value[0]['id'],
					default: 1
				})
				.then((res) => {
					if (res.code) {
						_this.dataForm[type] = 1;
						this.showSelect = false;
					}
				});
		},
		cancleSelect() {
			let _this = this;
			let type = this.selectItem;
			_this[type + '_loading'] = false;
			_this.showSelect = false;
		}
	}
};
</script>

<style lang="scss" scoped>
.container {
	background: #fff;
}
.btn {
	margin-top: 20rpx;
	padding-bottom: 20rpx;
}
</style>
